# 使用官方Python镜像作为基础
FROM python:3.9-slim as builder

# 安装构建依赖
RUN apt-get update && \
    apt-get install -y --no-install-recommends gcc python3-dev

# 设置工作目录
WORKDIR /app

# 先复制requirements文件单独安装依赖
COPY requirements.txt .
RUN pip install --user --no-cache-dir -r requirements.txt

# 第二阶段：运行时镜像
FROM python:3.9-slim

# 设置工作目录
WORKDIR /app

# 从builder阶段复制已安装的Python包
COPY --from=builder /root/.local /root/.local
COPY --from=builder /app/requirements.txt .

# 复制项目文件
COPY . .

# 确保脚本可执行
RUN chmod +x run.py

# 添加PATH以便能够找到用户安装的包
ENV PATH=/root/.local/bin:$PATH \
    PYTHONUNBUFFERED=1 \
    PYTHONPATH=/app

# 创建数据卷目录
RUN mkdir -p /app/data /app/hp_fixar_v01/data

# 暴露应用端口
EXPOSE 7777

# 健康检查
HEALTHCHECK --interval=30s --timeout=3s \
    CMD curl -f http://localhost:7777/ || exit 1

# 启动命令
CMD ["python", "run.py"]

# 数据卷配置
VOLUME ["/app/data", "/app/hp_fixar_v01/data"]